package com.bsoft.gol.hcs.clinical.dao;

import com.bsoft.gol.hcs.clinical.bean.req.FindTreatmentApplicationListRequest;
import com.bsoft.gol.hcs.clinical.bean.response.FindTreatmentApplicationListResp;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.util.converter.ConversionUtils;
import hcn.base.TreatmentApplicationRecord;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class TreatmentApplicationRecordDAO extends HibernateSupportDelegateDAO<TreatmentApplicationRecord> {

    @DAOMethod
    public abstract TreatmentApplicationRecord getByTreatmentId(String treatmentId);

    @DAOMethod(sql = "delete from TreatmentApplicationRecord where treatmentId = :treatmentId")
    public abstract void deleteByTreatmentId(@DAOParam("treatmentId") String treatmentId);

    @DAOMethod(sql = "update TreatmentApplicationRecord set applicationNo=:applicationNo  where  treatmentId =:treatmentId ")
    public abstract void updateApplicationById( @DAOParam("applicationNo") String applicationNo,@DAOParam("treatmentId")String treatmentId);

    public List<FindTreatmentApplicationListResp> findTreatmentApplicationList(FindTreatmentApplicationListRequest request){

          AbstractHibernateStatelessResultAction<List<FindTreatmentApplicationListResp>> action = new AbstractHibernateStatelessResultAction<List<FindTreatmentApplicationListResp>>() {
              @Override
              public void execute(StatelessSession ss) throws Exception {
                  StringBuilder builder = new StringBuilder();
                  builder.append("select dr.state treatStatus, tr.treatment_id  treatmentId, tr.application_no applicationNo,tr.req_time reqTime , td.treatment_item_name treatmentItemName,td.treatment_course treatmentCourse,td.treat_times_number treatTimesNumber " +
                          "from treatment_application_record tr,treatment_application_record_detail td,disposal_expense_record dr where tr.treatment_id=td.treatment_id  and tr.treatment_id=dr.itemId ");
                  if (StringUtils.isNotEmpty(request.getPatientId())){
                      builder.append(" and tr.patient_id = :patientId ");
                  }
                  if (StringUtils.isNotEmpty(request.getOrganizationId())){
                      builder.append(" and tr.organization_id = :organizationId");
                  }
                  if (StringUtils.isNotEmpty(request.getConsultId())){
                      builder.append(" and tr.consult_id = :consultId");
                  }
                  if (StringUtils.isNotEmpty(request.getTreatStatus())){
                      builder.append(" and dr.state = :treatStatus");
                  }
                  SQLQuery sqlQuery = ss.createSQLQuery(builder.toString());
                  sqlQuery.setProperties(request);
                  sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                  List<Map> list = sqlQuery.list();
                  List<FindTreatmentApplicationListResp> data=new ArrayList<>();
                  for (Map map:list) {
                      FindTreatmentApplicationListResp convert = ConversionUtils.convert(map, FindTreatmentApplicationListResp.class);
                      data.add(convert);
                  }
                    setResult(data);
              }
          };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public BigInteger countByStatus(final String state,final String patientId){

        AbstractHibernateStatelessResultAction<BigInteger> action = new AbstractHibernateStatelessResultAction<BigInteger>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder builder = new StringBuilder();
                builder.append("select count(1) from treatment_application_record tr ,disposal_expense_record dr where tr.treatment_id=dr.itemId ");
                if (StringUtils.isNotEmpty(patientId)){
                    builder.append(" and tr.patient_id = :patientId ");
                }

                if (StringUtils.isNotEmpty(state)){
                    builder.append(" and dr.state = :state");
                }
                SQLQuery sqlQuery = ss.createSQLQuery(builder.toString());
                sqlQuery.setParameter("state",state);
                sqlQuery.setParameter("patientId",patientId);
                List list = sqlQuery.list();
                if (list.size()==1){
                    Object o = list.get(0);
                    BigInteger count=(BigInteger)o;
                    setResult(count);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
